/***************************************************************************
 *   Copyright (C) 2010 by programmeboy                                    *
 *   programmeboy@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * 
 ***************************************************************************/
 
#include <ntifs.h>

#include "common.h"
#include "memory.h"

NTSTATUS 
ReadKernelMemory(
    OUT PVOID OutBuffer,
    IN PVOID Address,
    IN ULONG Size
)
/*++

Routine Description:

    This function used to read the memory to specified address

Arguments:

	Address - The specified address that to read
    Size - The size to read
    OutBuffer - The output buffer

Return Value:
    
    NTSTATUS

--*/
{
    NTSTATUS status;
    PMDL  pMdl;
    PVOID MdlAddress;

    if (!Address) 
        return STATUS_UNSUCCESSFUL;
    pMdl = IoAllocateMdl(Address, Size, FALSE, FALSE, 0);
    if (pMdl)
    {
        MmBuildMdlForNonPagedPool(pMdl);
        MdlAddress = MmGetSystemAddressForMdlSafe(pMdl, NormalPagePriority);
        if (MdlAddress){
            try{
                RtlCopyMemory(OutBuffer, MdlAddress, Size);
                status = STATUS_SUCCESS;
            }except(EXCEPTION_EXECUTE_HANDLER){
                NOTHING;
            }
        }            
        IoFreeMdl(pMdl);
    }
    return status;
}

NTSTATUS 
WriteKernelMemory(
    IN PVOID Address, 
    IN ULONG Size, 
    IN PVOID InBuffer
)
/*++

Routine Description:

    This function used to write the memory to specified address

Arguments:

    Address - The specified address that to write
    Size - The size to write
    OutBuffer - The input buffer

Return Value:

    NTSTATUS

--*/
{
    NTSTATUS status;
    PMDL  pMdl;
    PVOID MdlAddress;

    if (!Address) 
        return STATUS_UNSUCCESSFUL;
    pMdl = IoAllocateMdl(Address, Size, FALSE, FALSE, 0);
    if (pMdl){
        MmBuildMdlForNonPagedPool(pMdl);
        MdlAddress = MmGetSystemAddressForMdlSafe(pMdl, NormalPagePriority);
        if (MdlAddress){
            __try{
                WpOff();
                RtlCopyMemory(MdlAddress, InBuffer, Size);
                WpOn();
                status = STATUS_SUCCESS;
            }__except(EXCEPTION_EXECUTE_HANDLER){
                NOTHING;
            }
        }
        IoFreeMdl(pMdl);
    }
    return status;
}